<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="编码过程中，踩了一些小坑，做下记录：  1.全局变量count与std:count矛盾，建议用其他变量名。 2.内存泄漏问题 注意空间要开够 指针不可越界 main函数内开辟的栈空间大小一般为8MB 若要开辟较大的数组 请去main函数之外 3.编译器错误 推荐大家使用教新的较稳定的编译器 4.文件操作  打开后记得关闭 否则会占用系统资源 5.申请完空间，要记得释放，养成习惯。释放函数不可张冠李">
<meta property="og:type" content="article">
<meta property="og:title" content="文件的哈夫曼编码与解码">
<meta property="og:url" content="https://cheung0-bit.github.io/8a037252eb03/index.html">
<meta property="og:site_name" content="Bruce Zhang&#39;s Blogs">
<meta property="og:description" content="编码过程中，踩了一些小坑，做下记录：  1.全局变量count与std:count矛盾，建议用其他变量名。 2.内存泄漏问题 注意空间要开够 指针不可越界 main函数内开辟的栈空间大小一般为8MB 若要开辟较大的数组 请去main函数之外 3.编译器错误 推荐大家使用教新的较稳定的编译器 4.文件操作  打开后记得关闭 否则会占用系统资源 5.申请完空间，要记得释放，养成习惯。释放函数不可张冠李">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/6785a57e48e22ca91591508d3cac29ca.png">
<meta property="og:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/ba2e2c54061ec24a626112c02bd743a8.png">
<meta property="og:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/58c44a1746c80686ec3d57f94b26a4b3.png">
<meta property="og:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/4e1f9cf21d3682f1191b2af4651d924f.png">
<meta property="og:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/1853ca8790d22ce701fb602507979cfa.png">
<meta property="og:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/9a82c2bd2b2f60495e5d646f1f9d49ff.png">
<meta property="article:published_time" content="2021-12-19T13:18:27.000Z">
<meta property="article:modified_time" content="2022-11-25T07:07:19.746Z">
<meta property="article:author" content="张林">
<meta property="article:tag" content="数据结构与算法">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://0-bit.oss-cn-beijing.aliyuncs.com/6785a57e48e22ca91591508d3cac29ca.png">
    
    
      
        
          <link rel="shortcut icon" href="/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>文件的哈夫曼编码与解码</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
      <link rel="alternate" href="/atom.xml" title="Bruce Zhang&#39;s Blogs" type="application/atom+xml" />
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 7.0.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">Home</a></li><!--
     --><!--
       --><li><a href="/about/">About</a></li><!--
     --><!--
       --><li><a href="/archives/">Articles</a></li><!--
     --><!--
       --><li><a href="/categories/">Category</a></li><!--
     --><!--
       --><li><a href="/search/">Search</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="Previous post" href="/8d02adacbb72/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="Next post" href="/dfe61ddc5a2b/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="Back to top" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="Share post" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://cheung0-bit.github.io/8a037252eb03/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://cheung0-bit.github.io/8a037252eb03/&text=文件的哈夫曼编码与解码"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://cheung0-bit.github.io/8a037252eb03/&is_video=false&description=文件的哈夫曼编码与解码"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=文件的哈夫曼编码与解码&body=Check out this article: https://cheung0-bit.github.io/8a037252eb03/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://cheung0-bit.github.io/8a037252eb03/&name=文件的哈夫曼编码与解码&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://cheung0-bit.github.io/8a037252eb03/&t=文件的哈夫曼编码与解码"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E8%A6%81%E6%B1%82%E5%8F%8A%E4%BB%BB%E5%8A%A1%EF%BC%9A"><span class="toc-number">1.</span> <span class="toc-text">编码要求及任务：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A6%96%E5%85%88%EF%BC%8C%E5%87%86%E5%A4%87%E4%B8%80%E4%B8%AA%E6%BA%90%E6%96%87%E4%BB%B6"><span class="toc-number">1.1.</span> <span class="toc-text">首先，准备一个源文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E5%93%88%E5%A4%AB%E6%9B%BC%E8%8A%82%E7%82%B9"><span class="toc-number">1.2.</span> <span class="toc-text">构建哈夫曼节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%91%E6%95%B0%E7%BB%9F%E8%AE%A1"><span class="toc-number">1.3.</span> <span class="toc-text">频数统计</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%93%88%E5%A4%AB%E6%9B%BC%E7%BC%96%E7%A0%81"><span class="toc-number">1.4.</span> <span class="toc-text">哈夫曼编码</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B6%E4%B8%AD%EF%BC%8C%E5%AF%BB%E6%89%BE%E6%9C%80%E5%B0%8F%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%8F%B6%E5%AD%90%E8%8A%82%E7%82%B9%E5%8A%9F%E8%83%BD%E5%87%BD%E6%95%B0%E4%B8%BA"><span class="toc-number">1.4.1.</span> <span class="toc-text">其中，寻找最小的两个叶子节点功能函数为</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E5%B9%B6%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6"><span class="toc-number">1.5.</span> <span class="toc-text">编码并写入文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%93%E5%BC%80%E7%BC%96%E7%A0%81%E6%96%87%E4%BB%B6%EF%BC%8C%E8%BF%9B%E8%A1%8C%E8%A7%A3%E7%A0%81"><span class="toc-number">1.6.</span> <span class="toc-text">打开编码文件，进行解码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81"><span class="toc-number">1.7.</span> <span class="toc-text">二进制编码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="toc-number">2.</span> <span class="toc-text">运行结果</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E6%BA%90%E6%96%87%E4%BB%B6%EF%BC%8C%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81"><span class="toc-number">2.1.</span> <span class="toc-text">读取源文件，二进制编码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%9F%E8%AE%A1%E5%AD%97%E7%AC%A6%E9%A2%91%E6%AC%A1%EF%BC%8C%E5%BE%97%E5%88%B0%E7%BC%96%E7%A0%81%E8%A1%A8"><span class="toc-number">2.2.</span> <span class="toc-text">统计字符频次，得到编码表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E6%BA%90%E6%96%87%E4%BB%B6%E5%8F%8A%E7%BC%96%E7%A0%81%E7%BB%93%E6%9E%9C"><span class="toc-number">2.3.</span> <span class="toc-text">编码源文件及编码结果</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E7%A0%81%E7%BC%96%E7%A0%81%E6%96%87%E4%BB%B6%E5%8F%8A%E8%A7%A3%E7%A0%81%E7%BB%93%E6%9E%9C"><span class="toc-number">2.4.</span> <span class="toc-text">解码编码文件及解码结果</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8E%8B%E7%BC%A9%E6%95%88%E6%9E%9C"><span class="toc-number">2.5.</span> <span class="toc-text">压缩效果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E7%BB%93"><span class="toc-number">3.</span> <span class="toc-text">小结</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        文件的哈夫曼编码与解码
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">张林</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2021-12-19T13:18:27.000Z" itemprop="datePublished">2021-12-19</time>
        
        (Updated: <time datetime="2022-11-25T07:07:19.746Z" itemprop="dateModified">2022-11-25</time>)
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/%E9%9D%A2%E8%AF%95/">面试</a> › <a class="category-link" href="/categories/%E9%9D%A2%E8%AF%95/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/">数据结构与算法</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link-link" href="/tags/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E4%B8%8E%E7%AE%97%E6%B3%95/" rel="tag">数据结构与算法</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <p><strong>编码过程中，踩了一些小坑，做下记录：</strong></p>
<ul>
<li>1.全局变量<code>count</code>与<code>std:count</code>矛盾，建议用其他变量名。</li>
<li>2.内存泄漏问题 注意空间要开够 指针不可越界 <code>main</code>函数内开辟的栈空间大小一般为8MB 若要开辟较大的数组 请去<code>main</code>函数之外</li>
<li>3.编译器错误 推荐大家使用教新的较稳定的编译器</li>
<li>4.文件操作  打开后记得关闭 否则会占用系统资源</li>
<li>5.申请完空间，要记得释放，养成习惯。释放函数不可张冠李戴(留心编译器的<code>Warning</code>)。<code>malloc/free</code>,<code>new/delete</code>要配对使用。具体原因可参考 <a target="_blank" rel="noopener" href="https://blog.csdn.net/Dream_xun/article/details/50347139">这篇文章</a></li>
</ul>
<h3 id="编码要求及任务："><a href="#编码要求及任务：" class="headerlink" title="编码要求及任务："></a>编码要求及任务：</h3><p><strong>准备一个字符文件，要求：</strong></p>
<ol>
<li>统计该文件中各种字符的频率</li>
<li>对各字符进行 Huffman编码，显示每个字符的编码</li>
<li>以及将该文件翻译成 Huffman编码文件</li>
<li>再将 Huffman编码文件翻译成源文件</li>
<li>显示每个字符以一个字节进行二进制编码后的编码文件</li>
</ol>
<p><strong>实现步骤可分为：</strong></p>
<ol>
<li>统计被编码文件中个字符出现的频数，即统计权重</li>
<li>根据权重，构造哈夫曼树，进行哈夫曼编码</li>
<li>读取文件进行二进制编码</li>
<li>读取文件，将每个字符匹配哈夫曼编码，写入新文件，即完成编码</li>
<li>读取编码文件，根据哈夫曼编码进行解码，并写入新文件</li>
<li>对比二进制编码和哈夫曼编码后的文件字节大小，并计算压缩率</li>
</ol>
<h4 id="首先，准备一个源文件"><a href="#首先，准备一个源文件" class="headerlink" title="首先，准备一个源文件"></a>首先，准备一个源文件</h4><p>这里我准备了一首小诗，写入文件，并将其命名为<code>poem.txt</code></p>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">If I could save time in a bottle</span><br><span class="line">the first thing that I&#x27;d like to do</span><br><span class="line">is to save every day until eternity passes away</span><br><span class="line">just to spend them with you</span><br><span class="line">if I could make days last forever</span><br><span class="line">if words could make wishes come true</span><br><span class="line">I&#x27;d save every day like a treasure and then</span><br><span class="line">again I would spend them with you</span><br></pre></td></tr></table></figure>



<h4 id="构建哈夫曼节点"><a href="#构建哈夫曼节点" class="headerlink" title="构建哈夫曼节点"></a>构建哈夫曼节点</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义哈夫曼树节点</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> &#123;</span><br><span class="line">	<span class="type">int</span> weight;</span><br><span class="line">	<span class="type">int</span> parent;</span><br><span class="line">	<span class="type">int</span> l_child;</span><br><span class="line">	<span class="type">int</span> r_child;</span><br><span class="line">	<span class="type">char</span> data;</span><br><span class="line">&#125; HTNode, * HuffmanTree;</span><br><span class="line"><span class="keyword">typedef</span> <span class="type">char</span>** HuffmanCode;</span><br></pre></td></tr></table></figure>

<h4 id="频数统计"><a href="#频数统计" class="headerlink" title="频数统计"></a>频数统计</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//统计该文件中各种字符的频率</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">frequencyRecord</span><span class="params">(HuffmanTree&amp; HT)</span> </span>&#123;</span><br><span class="line">	HuffmanTree TEMP;</span><br><span class="line">	TEMP = <span class="keyword">new</span> HTNode[<span class="number">130</span>];</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">130</span>; ++i) &#123;</span><br><span class="line">		TEMP[i].weight = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function">ifstream <span class="title">originFile</span><span class="params">(<span class="string">&quot;poem.txt&quot;</span>)</span></span>;</span><br><span class="line">	originFile.<span class="built_in">seekg</span>(<span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span> (!originFile) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Can&#x27;t find the file!&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="type">char</span> _data;</span><br><span class="line">		cin.<span class="built_in">unsetf</span>(ios::skipws);</span><br><span class="line">		<span class="keyword">while</span> (!originFile.<span class="built_in">eof</span>()) &#123;</span><br><span class="line">			<span class="keyword">if</span> (originFile.<span class="built_in">get</span>(_data)) &#123;</span><br><span class="line">				TEMP[_data].data = _data;</span><br><span class="line">				TEMP[_data].weight++;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		originFile.<span class="built_in">close</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">130</span>; ++i) &#123;</span><br><span class="line">		<span class="keyword">if</span> (TEMP[i].weight != <span class="number">0</span>) &#123;</span><br><span class="line">			N++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	HT = <span class="keyword">new</span> HTNode[<span class="number">2</span> * N];</span><br><span class="line">	<span class="type">int</span> k = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">130</span>; ++i) &#123;</span><br><span class="line">		<span class="keyword">if</span> (TEMP[i].weight != <span class="number">0</span>) &#123;</span><br><span class="line">			HT[k++] = TEMP[i];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="哈夫曼编码"><a href="#哈夫曼编码" class="headerlink" title="哈夫曼编码"></a>哈夫曼编码</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//对各字符进行 Huffman编码，显示每个字符的编码</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">HuffmanCoding</span><span class="params">(HuffmanTree&amp; HT, HuffmanCode&amp; HC)</span> </span>&#123;</span><br><span class="line">	<span class="type">int</span> m = <span class="number">2</span> * N - <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= N; ++i) &#123;</span><br><span class="line">		HT[i].parent = <span class="number">0</span>;</span><br><span class="line">		HT[i].l_child = <span class="number">0</span>;</span><br><span class="line">		HT[i].r_child = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = N + <span class="number">1</span>; i &lt;= m; ++i) &#123;</span><br><span class="line">		HT[i].weight = <span class="number">0</span>;</span><br><span class="line">		HT[i].parent = <span class="number">0</span>;</span><br><span class="line">		HT[i].l_child = <span class="number">0</span>;</span><br><span class="line">		HT[i].r_child = <span class="number">0</span>;</span><br><span class="line">		HT[i].data = <span class="string">&#x27;#&#x27;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="type">int</span> child1, child2;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = N + <span class="number">1</span>; i &lt;= m; i++) &#123;</span><br><span class="line">		<span class="built_in">select</span>(HT, i - <span class="number">1</span>, child1, child2);</span><br><span class="line">		HT[child1].parent = i;</span><br><span class="line">		HT[child2].parent = i;</span><br><span class="line">		HT[i].l_child = child1;</span><br><span class="line">		HT[i].r_child = child2;</span><br><span class="line">		HT[i].weight = HT[child1].weight + HT[child2].weight;</span><br><span class="line">	&#125;</span><br><span class="line">	HC = <span class="keyword">new</span> <span class="type">char</span>* [N + <span class="number">1</span>];</span><br><span class="line">	<span class="type">char</span>* cd = <span class="keyword">new</span> <span class="type">char</span>[N];</span><br><span class="line">	cd[N - <span class="number">1</span>] = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">	<span class="type">int</span> start, c, f;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= N; i++) &#123;</span><br><span class="line">		start = N - <span class="number">1</span>;</span><br><span class="line">		<span class="keyword">for</span> (c = i, f = HT[i].parent; f != <span class="number">0</span>; c = f, f = HT[f].parent) &#123;</span><br><span class="line">			<span class="keyword">if</span> (HT[f].l_child == c) cd[--start] = <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">			<span class="keyword">else</span> cd[--start] = <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		HC[i] = <span class="keyword">new</span> <span class="type">char</span>[N - start];</span><br><span class="line">		<span class="built_in">strcpy</span>(HC[i], &amp;cd[start]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">delete</span>[] cd;</span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= N; i++) &#123;</span><br><span class="line">		<span class="keyword">if</span> (HT[i].data == <span class="string">&#x27;\n&#x27;</span>) &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;回车&quot;</span> &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; HC[i] &lt;&lt; endl;</span><br><span class="line">		&#125; <span class="keyword">else</span> <span class="keyword">if</span> (HT[i].data == <span class="string">&#x27; &#x27;</span>) &#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;空格&quot;</span> &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; HC[i] &lt;&lt; endl;</span><br><span class="line">		&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">			cout &lt;&lt; HT[i].data &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; HC[i] &lt;&lt; endl;;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="其中，寻找最小的两个叶子节点功能函数为"><a href="#其中，寻找最小的两个叶子节点功能函数为" class="headerlink" title="其中，寻找最小的两个叶子节点功能函数为"></a>其中，寻找最小的两个叶子节点功能函数为</h5><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//找出最小的两个叶子节点</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">select</span><span class="params">(HuffmanTree HT, <span class="type">int</span> num, <span class="type">int</span>&amp; child1, <span class="type">int</span>&amp; child2)</span> </span>&#123;</span><br><span class="line">	child1 = child2 = <span class="number">0</span>;</span><br><span class="line">	<span class="type">int</span> w1 = <span class="number">0</span>, w2 = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">//Start finding...</span></span><br><span class="line">	<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= num; ++i) &#123;</span><br><span class="line">		<span class="keyword">if</span> (HT[i].parent == <span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">if</span> (child1 == <span class="number">0</span>) &#123;</span><br><span class="line">				child1 = i;</span><br><span class="line">				w1 = HT[i].weight;</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span> (child2 == <span class="number">0</span>) &#123;</span><br><span class="line">				child2 = i;</span><br><span class="line">				w2 = HT[i].weight;</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span> (w1 &gt; w2 &amp;&amp; w1 &gt; HT[i].weight) &#123;</span><br><span class="line">				child1 = i;</span><br><span class="line">				w1 = HT[i].weight;</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">if</span> (w2 &gt; w1 &amp;&amp; w2 &gt; HT[i].weight) &#123;</span><br><span class="line">				child2 = i;</span><br><span class="line">				w2 = HT[i].weight;</span><br><span class="line">				<span class="keyword">continue</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">// 使得w1永远小于w2</span></span><br><span class="line">	<span class="type">int</span> temp;</span><br><span class="line">	<span class="keyword">if</span> (w1 &gt; w2) &#123;</span><br><span class="line">		temp = child1;</span><br><span class="line">		child1 = child2;</span><br><span class="line">		child2 = temp;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="编码并写入文件"><a href="#编码并写入文件" class="headerlink" title="编码并写入文件"></a>编码并写入文件</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//将该文件翻译成 Huffman编码文件</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">zip</span><span class="params">(HuffmanTree&amp; HT, HuffmanCode&amp; HC, vector&lt;string&gt;&amp; code)</span> </span>&#123;</span><br><span class="line">	<span class="function">ofstream <span class="title">codeFile</span><span class="params">(<span class="string">&quot;codefile.txt&quot;</span>)</span></span>;</span><br><span class="line">	<span class="function">ifstream <span class="title">originFile</span><span class="params">(<span class="string">&quot;poem.txt&quot;</span>)</span></span>;</span><br><span class="line">	<span class="keyword">if</span> (!codeFile) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Can&#x27;t find the file!&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="type">char</span> _data;</span><br><span class="line">		cin.<span class="built_in">unsetf</span>(ios::skipws);</span><br><span class="line">		<span class="keyword">while</span> (!originFile.<span class="built_in">eof</span>()) &#123;</span><br><span class="line">			<span class="keyword">if</span> (originFile.<span class="built_in">get</span>(_data)) &#123;</span><br><span class="line">				<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= N; ++i) &#123;</span><br><span class="line">					<span class="keyword">if</span> (HT[i].data == _data) &#123;</span><br><span class="line">						codeFile &lt;&lt; HC[i];</span><br><span class="line">						code.<span class="built_in">push_back</span>(HC[i]);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	codeFile.<span class="built_in">close</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="打开编码文件，进行解码"><a href="#打开编码文件，进行解码" class="headerlink" title="打开编码文件，进行解码"></a>打开编码文件，进行解码</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//再将 Huffman编码文件翻译成源文件</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">unzip</span><span class="params">(HuffmanTree&amp; HT, HuffmanCode&amp; HC, vector&lt;string&gt;&amp; code)</span> </span>&#123;</span><br><span class="line">	<span class="function">ofstream <span class="title">decodeFile</span><span class="params">(<span class="string">&quot;decodefile.txt&quot;</span>)</span></span>;</span><br><span class="line">	<span class="keyword">if</span> (!decodeFile) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Can&#x27;t find the file!&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		vector&lt;string&gt;::iterator v = code.<span class="built_in">begin</span>();</span><br><span class="line">		<span class="keyword">while</span> (v != code.<span class="built_in">end</span>()) &#123;</span><br><span class="line">			<span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">1</span>; i &lt;= N; ++i) &#123;</span><br><span class="line">				<span class="keyword">if</span> (HC[i] == *v) &#123;</span><br><span class="line">					decodeFile &lt;&lt; HT[i].data;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			v++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	decodeFile.<span class="built_in">close</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="二进制编码"><a href="#二进制编码" class="headerlink" title="二进制编码"></a>二进制编码</h4><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//显示每个字符以一个字节进行二进制编码后的编码文件</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">binaryCode</span><span class="params">()</span> </span>&#123;</span><br><span class="line">	<span class="function">ofstream <span class="title">binaryFile</span><span class="params">(<span class="string">&quot;binaryfile.txt&quot;</span>)</span></span>;</span><br><span class="line">	<span class="function">ifstream <span class="title">originFile</span><span class="params">(<span class="string">&quot;poem.txt&quot;</span>)</span></span>;</span><br><span class="line">	originFile.<span class="built_in">seekg</span>(<span class="number">0</span>);</span><br><span class="line">	<span class="keyword">if</span> (!originFile) &#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;Can&#x27;t find the file!&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="type">char</span> _data;</span><br><span class="line">		cin.<span class="built_in">unsetf</span>(ios::skipws);</span><br><span class="line">		<span class="keyword">while</span> (!originFile.<span class="built_in">eof</span>()) &#123;</span><br><span class="line">			<span class="keyword">if</span> (originFile.<span class="built_in">get</span>(_data)) &#123;</span><br><span class="line">				<span class="function">bitset&lt;8&gt; <span class="title">data</span><span class="params">(_data)</span></span>;</span><br><span class="line">				binaryFile &lt;&lt; data;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		originFile.<span class="built_in">close</span>();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="运行结果"><a href="#运行结果" class="headerlink" title="运行结果"></a>运行结果</h3><h4 id="读取源文件，二进制编码"><a href="#读取源文件，二进制编码" class="headerlink" title="读取源文件，二进制编码"></a>读取源文件，二进制编码</h4><p><img src="https://0-bit.oss-cn-beijing.aliyuncs.com/6785a57e48e22ca91591508d3cac29ca.png" alt="image-20211219230708380"></p>
<h4 id="统计字符频次，得到编码表"><a href="#统计字符频次，得到编码表" class="headerlink" title="统计字符频次，得到编码表"></a>统计字符频次，得到编码表</h4><p><img src="https://0-bit.oss-cn-beijing.aliyuncs.com/ba2e2c54061ec24a626112c02bd743a8.png" alt="image-20211219230742495"></p>
<h4 id="编码源文件及编码结果"><a href="#编码源文件及编码结果" class="headerlink" title="编码源文件及编码结果"></a>编码源文件及编码结果</h4><p><img src="https://0-bit.oss-cn-beijing.aliyuncs.com/58c44a1746c80686ec3d57f94b26a4b3.png" alt="image-20211219230803887"></p>
<h4 id="解码编码文件及解码结果"><a href="#解码编码文件及解码结果" class="headerlink" title="解码编码文件及解码结果"></a>解码编码文件及解码结果</h4><p><img src="https://0-bit.oss-cn-beijing.aliyuncs.com/4e1f9cf21d3682f1191b2af4651d924f.png" alt="image-20211219230819193"></p>
<h4 id="压缩效果"><a href="#压缩效果" class="headerlink" title="压缩效果"></a>压缩效果</h4><p><img src="https://0-bit.oss-cn-beijing.aliyuncs.com/1853ca8790d22ce701fb602507979cfa.png" alt="image-20211219230828908"></p>
<p><img src="https://0-bit.oss-cn-beijing.aliyuncs.com/9a82c2bd2b2f60495e5d646f1f9d49ff.png" alt="image-20211219231051048"></p>
<p>发现这里实际大小与占用空间不一样？<a target="_blank" rel="noopener" href="https://blog.csdn.net/buptman1/article/details/24200863">这篇文章</a>可以解答你的疑惑</p>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><p>通过编写利用哈夫曼算法实现的文件编码解码小工具，可加深对哈夫曼算法的理解，以及编码的熟练度。</p>
<p>同时，体会到通过算法减少文本空间，降低计算机磁盘负荷的妙处，我们需要优秀的算法来提升计算机性能。在实际的压缩算法中虽然很少听到哈夫曼编码，但其实它并没有被淘汰，而是作为核心的算法，结合了其他的压缩算法，实现对文件（文本，PPT,图片，电影等）的压缩，给日常生活带来极大便利。</p>
<p>本人的小工具仅针对英文大小字母及<code>&#39;</code> <code>&#39;\n&#39;</code> <code>&#39;  &#39;</code>字符针对性的进行了哈夫曼编码，若想实现中文及各种支持语言的编码，可在此代码基础上，进行优化，源码地址为<a target="_blank" rel="noopener" href="https://github.com/Cheung0-bit/HuffmanTreeCoding">https://github.com/Cheung0-bit/HuffmanTreeCoding</a>，欢迎PR；</p>

  </div>
</article>


    <div class="blog-post-comments">
        <div id="utterances_thread">
            <noscript>Please enable JavaScript to view the comments.</noscript>
        </div>
    </div>


        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Articles</a></li>
         
          <li><a href="/categories/">Category</a></li>
         
          <li><a href="/search/">Search</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E8%A6%81%E6%B1%82%E5%8F%8A%E4%BB%BB%E5%8A%A1%EF%BC%9A"><span class="toc-number">1.</span> <span class="toc-text">编码要求及任务：</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A6%96%E5%85%88%EF%BC%8C%E5%87%86%E5%A4%87%E4%B8%80%E4%B8%AA%E6%BA%90%E6%96%87%E4%BB%B6"><span class="toc-number">1.1.</span> <span class="toc-text">首先，准备一个源文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9E%84%E5%BB%BA%E5%93%88%E5%A4%AB%E6%9B%BC%E8%8A%82%E7%82%B9"><span class="toc-number">1.2.</span> <span class="toc-text">构建哈夫曼节点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%91%E6%95%B0%E7%BB%9F%E8%AE%A1"><span class="toc-number">1.3.</span> <span class="toc-text">频数统计</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%93%88%E5%A4%AB%E6%9B%BC%E7%BC%96%E7%A0%81"><span class="toc-number">1.4.</span> <span class="toc-text">哈夫曼编码</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%B6%E4%B8%AD%EF%BC%8C%E5%AF%BB%E6%89%BE%E6%9C%80%E5%B0%8F%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%8F%B6%E5%AD%90%E8%8A%82%E7%82%B9%E5%8A%9F%E8%83%BD%E5%87%BD%E6%95%B0%E4%B8%BA"><span class="toc-number">1.4.1.</span> <span class="toc-text">其中，寻找最小的两个叶子节点功能函数为</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E5%B9%B6%E5%86%99%E5%85%A5%E6%96%87%E4%BB%B6"><span class="toc-number">1.5.</span> <span class="toc-text">编码并写入文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%89%93%E5%BC%80%E7%BC%96%E7%A0%81%E6%96%87%E4%BB%B6%EF%BC%8C%E8%BF%9B%E8%A1%8C%E8%A7%A3%E7%A0%81"><span class="toc-number">1.6.</span> <span class="toc-text">打开编码文件，进行解码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81"><span class="toc-number">1.7.</span> <span class="toc-text">二进制编码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C"><span class="toc-number">2.</span> <span class="toc-text">运行结果</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AF%BB%E5%8F%96%E6%BA%90%E6%96%87%E4%BB%B6%EF%BC%8C%E4%BA%8C%E8%BF%9B%E5%88%B6%E7%BC%96%E7%A0%81"><span class="toc-number">2.1.</span> <span class="toc-text">读取源文件，二进制编码</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BB%9F%E8%AE%A1%E5%AD%97%E7%AC%A6%E9%A2%91%E6%AC%A1%EF%BC%8C%E5%BE%97%E5%88%B0%E7%BC%96%E7%A0%81%E8%A1%A8"><span class="toc-number">2.2.</span> <span class="toc-text">统计字符频次，得到编码表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E6%BA%90%E6%96%87%E4%BB%B6%E5%8F%8A%E7%BC%96%E7%A0%81%E7%BB%93%E6%9E%9C"><span class="toc-number">2.3.</span> <span class="toc-text">编码源文件及编码结果</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E7%A0%81%E7%BC%96%E7%A0%81%E6%96%87%E4%BB%B6%E5%8F%8A%E8%A7%A3%E7%A0%81%E7%BB%93%E6%9E%9C"><span class="toc-number">2.4.</span> <span class="toc-text">解码编码文件及解码结果</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8E%8B%E7%BC%A9%E6%95%88%E6%9E%9C"><span class="toc-number">2.5.</span> <span class="toc-text">压缩效果</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E7%BB%93"><span class="toc-number">3.</span> <span class="toc-text">小结</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://cheung0-bit.github.io/8a037252eb03/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://cheung0-bit.github.io/8a037252eb03/&text=文件的哈夫曼编码与解码"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://cheung0-bit.github.io/8a037252eb03/&is_video=false&description=文件的哈夫曼编码与解码"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=文件的哈夫曼编码与解码&body=Check out this article: https://cheung0-bit.github.io/8a037252eb03/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://cheung0-bit.github.io/8a037252eb03/&title=文件的哈夫曼编码与解码"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://cheung0-bit.github.io/8a037252eb03/&name=文件的哈夫曼编码与解码&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://cheung0-bit.github.io/8a037252eb03/&t=文件的哈夫曼编码与解码"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
      
        
          2022-2024
            <a target="_blank" rel="noopener" href="https://beian.miit.gov.cn/">
              苏ICP备2022044873号
            </a>
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        
          <!--
       -->
          <li><a href="/">
              Home
            </a></li>
          <!--
     -->
          
          <!--
       -->
          <li><a href="/about/">
              About
            </a></li>
          <!--
     -->
          
          <!--
       -->
          <li><a href="/archives/">
              Articles
            </a></li>
          <!--
     -->
          
          <!--
       -->
          <li><a href="/categories/">
              Category
            </a></li>
          <!--
     -->
          
          <!--
       -->
          <li><a href="/search/">
              Search
            </a></li>
          <!--
     -->
          
      </ul>
    </nav>
  </div>
</footer>
    </div>
    <!-- styles -->


 
  <link
    rel="preload"
    href="/lib/font-awesome/css/all.min.css"
    as="style"
    onload="this.onload=null;this.rel='stylesheet'"
  />
  <noscript
    ><link
      rel="stylesheet"
      href="/lib/font-awesome/css/all.min.css"
  /></noscript>



    <!-- jquery -->
 
  
<script src="/lib/jquery/jquery.min.js"></script>





<!-- clipboard -->

   
    
<script src="/lib/clipboard/clipboard.min.js"></script>

  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

  <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?4484a4a33014b59670c470a6e15a66db";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
        </script>

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

    <script type="text/javascript">
      var utterances_repo = 'Cheung0-bit/blog-package';
      var utterances_issue_term = 'title';
      var utterances_label = 'Comment';
      var utterances_theme = 'github-dark';

      (function(){
          var script = document.createElement('script');

          script.src = 'https://utteranc.es/client.js';
          script.setAttribute('repo', utterances_repo);
          script.setAttribute('issue-term', 'pathname');
          script.setAttribute('label', utterances_label);
          script.setAttribute('theme', utterances_theme);
          script.setAttribute('crossorigin', 'anonymous');
          script.async = true;
          (document.getElementById('utterances_thread')).appendChild(script);
      }());
  </script>

</body>
</html>
